package cn.kxm.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.kxm.dao.InstroageBillDetailMapper;
import cn.kxm.dao.InstroageBillMapper;
import cn.kxm.pojo.InstroageBill;
import cn.kxm.pojo.InstroageBillDetail;
import cn.kxm.service.InstroageBillDetailService;
import cn.kxm.service.InstroageBillService;
@Service
public class InstroageBillServiceImpl implements InstroageBillService{

//User by instroagebill
    @Autowired
	private InstroageBillMapper instroageBillMapper;
    @Autowired
    private InstroageBillDetailService detailService;
    @Autowired
    private InstroageBillDetailMapper detailMapper;
	@Override
	public int findInstroageBillCount() {
		return instroageBillMapper.findInstroageBillCount();
	}
	@Override
	public List<InstroageBill> getInstroageBillList(Integer page,
			Integer size, String findcode, Date findstartDate,
			Date findendDate, String findsupplierName) {
		int currPage=(page-1)*size;
		return instroageBillMapper.getInstroageBillList(currPage, size, findcode, findstartDate, findendDate, findsupplierName);
	}
	@Override
	public boolean addInstroageBill(String inBill,String codes,Double total) {
		JSONObject inBillJson=JSONObject.parseObject(inBill);
		InstroageBill instroageBill=new InstroageBill();
		instroageBill.setCode((String)inBillJson.get("code"));
		instroageBill.setCustomName((String)inBillJson.get("customName"));
		try {
			instroageBill.setInstroageDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)inBillJson.get("instroageDate")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		instroageBill.setInstroageType(inBillJson.getInteger("instroageType"));
		instroageBill.setLinkman((String)inBillJson.get("linkman"));
		instroageBill.setMemo((String)inBillJson.get("memo"));
		instroageBill.setPhone((String)inBillJson.get("phone"));
		instroageBill.setMoney(total);
		if(instroageBillMapper.addInstroageBill(instroageBill)!=1){
			return false;
		}
		JSONArray backBodyJson = JSONArray.parseArray(codes);
		for(int i=0;i<backBodyJson.size();i++){
			InstroageBillDetail detail=new InstroageBillDetail();
			JSONObject code=backBodyJson.getJSONObject(i);
			detail.setAmount((Integer)code.get("amount"));
			detail.setBillCode((String)instroageBill.getCode());
			detail.setMemo((String)code.get("memo"));
			detail.setPartCode((String)code.get("code"));
			detail.setPrice(code.getDoubleValue("price"));
			if (detailService.addInstroageBillDetail(detail)!=1) {
				return false;
			}
		}
		
		return true;
	}
	@Override
	public InstroageBill getInstroageBillByCode(String code) {
		return instroageBillMapper.getInstroageBillByCode(code);
	}
	@Override
	public boolean modifyInstroageBill(String inBill,String codes,Double total) {
		JSONObject inBillJson=JSONObject.parseObject(inBill);
		InstroageBill instroageBill=new InstroageBill();
		instroageBill.setCode((String)inBillJson.get("code"));
		instroageBill.setCustomName((String)inBillJson.get("customName"));
		try {
			instroageBill.setInstroageDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)inBillJson.get("instroageDate")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		instroageBill.setInstroageType(inBillJson.getInteger("instroageType"));
		instroageBill.setLinkman((String)inBillJson.get("linkman"));
		instroageBill.setMemo((String)inBillJson.get("memo"));
		instroageBill.setPhone((String)inBillJson.get("phone"));
		instroageBill.setMoney(total);
		if(instroageBillMapper.updateInstroageBill(instroageBill)!=1){
			return false;
		}
		if(detailService.delDetailByUpdate(instroageBill.getCode())<=0){
			return false;
		}
		JSONArray backBodyJson = JSONArray.parseArray(codes);
		for(int i=0;i<backBodyJson.size();i++){
			InstroageBillDetail detail=new InstroageBillDetail();
			JSONObject code=backBodyJson.getJSONObject(i);
			/*if(code.get("amount") instanceof String){
				detail.setAmount(Integer.valueOf((String)code.get("amount")));
			}else{
				detail.setAmount((Integer)code.get("amount"));
			}*/
			detail.setAmount(code.getIntValue("amount"));
			detail.setBillCode((String)instroageBill.getCode());
			detail.setMemo((String)code.get("memo"));
			detail.setPartCode((String)code.get("code"));
			detail.setPrice(code.getDoubleValue("price"));
			if (detailService.addInstroageBillDetail(detail)!=1) {
				return false;
			}
		}
		return true;
	}
	@Override
	public boolean delInstroageBillCode(String code) {
		if(detailService.delInstroageBillDetailByBillCode(code)<=0){
			return false;
		}
		if(instroageBillMapper.delInstroageBillCode(code)<=0){
			return false;
		}
		return true;
	}
	@Override
	public boolean delInstroageBillByArray(String[] codes) {
		if(detailMapper.removeInstroageBillDetailByArray(codes)<=0){
			return false;
		}
		if(instroageBillMapper.delInstroageBillByArray(codes)<=0){
			return false;
		}
		return true;
	}
	
	//User by payBill
		@Autowired
	InstroageBillDao instroageBillDao;
	
	@Autowired
	PayBillDetailDao payBillDetailDao;

	/* (non-Javadoc)
	 * @see cn.kxm.service.InstroageBillService#findPayBillDetail(java.lang.String)
	 */
	@Override
	public InstroageBill findPayBillDetail(String payCode) {
		return instroageBillDao.findInstroageBillByBillCode(payCode);
	}

	/* (non-Javadoc)
	 * @see cn.kxm.service.InstroageBillService#updateInstroageBillType(java.lang.String, java.lang.Integer)
	 */
	@Override
	public void updateInstroageBillType(String code, Integer type) {
		instroageBillDao.updateInstroageBillType(code, type);
	}

	/* (non-Javadoc)
	 * @see cn.kxm.service.InstroageBillService#findUnselectedInstroageBill(java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public List<InstroageBill> findUnselectedInstroageBill(Integer page, Integer rows,
			String findcode,
			String findstartDate,
			String findendDate,
			String findsupplierName,
			String payCode) {
		return instroageBillDao.findUnselectedInstroageBill(((page-1)*rows), rows,findcode,findstartDate,findendDate,findsupplierName,payCode);
	}

	/* (non-Javadoc)
	 * @see cn.kxm.service.InstroageBillService#countUnselectedInstroageBill()
	 */
	@Override
	public int countUnselectedInstroageBill(String findcode,
			String findstartDate,
			String findendDate,
			String findsupplierName,
			String payCode) {
		return instroageBillDao.countUnselectedInstroageBill(findcode,findstartDate,findendDate,findsupplierName,payCode);
	}

	/* (non-Javadoc)
	 * @see cn.kxm.service.InstroageBillService#findInstroageBillByPayBillCode(java.lang.String, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public List<InstroageBill> findInstroageBillByPayBillCode(String payCode) {
		List<String> list=payBillDetailDao.findBillCodeByPayCode(payCode);
		return instroageBillDao.findInstroageBillByPayBillCode(list);
	

}
